package net.chathu.airgw.model 
{
	import flash.utils.Dictionary;
	import net.chathu.airgw.util.StringUtil;
	/**
	 * ...
	 * @author ze
	 */
	public class RoomList 
	{
		
		private var roomdict:Dictionary;
		private static const regExp:RegExp = new RegExp("[^a-z^A-Z^0-9^\\-^_^|^(^)]","g");
		
		public function RoomList() 
		{
			roomdict = new Dictionary();
		}
		
		public function add(room:Room):Boolean
		{
			if (!contains(room))
			{
				var roomIrcName:String  =  (room.isCustomRoom ? "&" : "#") + roomNameToIrcFormat(room.name);
				var counter:int = 0;
				var prevRoom:Room;
				var tempRoomName:String = roomIrcName;
				while ((prevRoom = getRoomByName(roomIrcName)) != null)
				{
					roomIrcName = tempRoomName + (counter++);
				}
				room.name = roomIrcName;
				//trace("room added: " + room.name);
				roomdict[room.id] = room;
				return true;
			} else {
				return false;
			}
		}
		
		public function remove(room:Room):Boolean
		{
			if (contains(room))
			{
				delete roomdict[room.id];
				return true;
			} else {
				return false;
			}
		}
		
		public function contains(room:Room):Boolean
		{
			return !!(roomdict[room.id]);
		}
		
		public function clear():void
		{
			roomdict = new Dictionary();	
		}
		
		public function getRoom(id:int):Room
		{
			return roomdict[id];		
		}
		
		public function getRoomByInviteID(inviteid:int):Room
		{
			for (var key:* in roomdict)
			{
				var room:Room = roomdict[key];
				if (room.inviteId == inviteid)
				{
					return room;
				}
			}	
			return null;
		}
				
		public function getRoomNum():int
		{
			var n:int = 0;
			for (var key:* in roomdict)
			{
				n++;
			}	
			return n;
		}
		
		public function getRoomByName(name:String):Room
		{
			
			for each (var room:Room in roomdict)
			{
				if (room.name == name)
				{
					return room;
				}
			}	
			return null;
		}
		
		public function getRoomList():Dictionary {
			return roomdict;
		}
		
		public static function roomNameToIrcFormat(roomName:String):String {
			
			var temp:String=roomName;
		
			temp = StringUtil.replaceAll(temp, 'á', 'a');
			temp = StringUtil.replaceAll(temp, 'é', 'e');
			temp = StringUtil.replaceAll(temp, 'í', 'i');
			
			temp = StringUtil.replaceAll(temp, 'ú', 'u');
			temp = StringUtil.replaceAll(temp, 'ü', 'u');
			temp = StringUtil.replaceAll(temp, 'ű', 'u');
			
			temp = StringUtil.replaceAll(temp, 'ó', 'o');
			temp = StringUtil.replaceAll(temp, 'ö', 'o');
			temp = StringUtil.replaceAll(temp, 'ő', 'o');
			
			temp = StringUtil.replaceAll(temp, 'Á', 'A');
			temp = StringUtil.replaceAll(temp, 'É', 'E');
			temp = StringUtil.replaceAll(temp, 'Í', 'I');
			
			temp = StringUtil.replaceAll(temp, 'Ú', 'U');
			temp = StringUtil.replaceAll(temp, 'Ü', 'U');
			temp = StringUtil.replaceAll(temp, 'Ű', 'U');
			
			temp = StringUtil.replaceAll(temp, 'Ó', 'O');
			temp = StringUtil.replaceAll(temp, 'Ö', 'O');
			temp = StringUtil.replaceAll(temp, 'Ő', 'O');
		
			
			temp = temp.replace(regExp, "_");
			if (temp.indexOf("_") == 0)
			{
				//nem kezdodhet _ karakterrel
				 temp="room"+temp;
			}
			return temp;
		}
	}
}